#include "DxInput.h"

CDxInput::CDxInput()
{
	_directInput=NULL;
	_dxMouse=NULL;	
	_dxKeyboard=NULL;
	_mouse_X=0;
	_mouse_Y=0;
}

CDxInput::~CDxInput()
{
	if(_directInput!=NULL)
		_directInput->Release();
	if(_dxKeyboard!=NULL)
		_dxKeyboard->Release();
	if(_dxMouse!=NULL)
		_dxMouse->Release();
}
bool CDxInput::Init_DirectInput(HINSTANCE hInst,HWND hwnd)
{
	HRESULT result=DirectInput8Create(
		hInst,
		DIRECTINPUT_VERSION,
		IID_IDirectInput8,
		(void**)&_directInput,
		NULL);

	if(result!=DI_OK)
		return false;

	//initialize keyboard
	result=_directInput->CreateDevice(GUID_SysKeyboard,&_dxKeyboard,NULL);
	if(result!=DI_OK)
		return false;

	//initialize mouse
	result=_directInput->CreateDevice(GUID_SysMouse,&_dxMouse,NULL);
	if(result!=DI_OK)
		return false;
	return true;
}

bool CDxInput::Init_Keyboard(HWND hwnd)
{
	//set data format for keyboard input
	HRESULT result=_dxKeyboard->SetDataFormat(&c_dfDIKeyboard);
	if(result!=DI_OK)
		return false;
	//set cooperative
	result=_dxKeyboard->SetCooperativeLevel(hwnd,DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
	if(result!=DI_OK)
		return false;
	//acquire the keyboard
	result=_dxKeyboard->Acquire();
	if(result!=DI_OK)
		return false;

	return true;
}
void CDxInput::Pool_Keyboard()
{
	for(int i=0;i<256;i++)
	{
		_oldKeys[i]=_keys[i];
	}
	_dxKeyboard->Poll();
	_dxKeyboard->GetDeviceState(sizeof(_keys),(LPVOID)&_keys);
	ReAcquireWhenLost();
}

void CDxInput::ReAcquireWhenLost()
{
	//keyboard
	HRESULT result;
	result=_dxKeyboard->GetDeviceState(sizeof(_keys),(LPVOID)&_keys);
	if(FAILED(result))
	{
		result=_dxKeyboard->Acquire();
		if(result==DIERR_INPUTLOST)
		{
			result=_dxKeyboard->Acquire();
		}
	}
	//mouse
	HRESULT hresult;
	hresult=_dxMouse->GetDeviceState(sizeof(_mouseState),(LPVOID)&_mouseState);
	if(FAILED(hresult))
	{
		hresult=_dxMouse->Acquire();
		if(result==DIERR_INPUTLOST)
		{
			hresult=_dxMouse->Acquire();
		}
	}
}
bool CDxInput::Init_Mouse(HWND hwnd)
{
	//set data format for mouse
	HRESULT result=_dxMouse->SetDataFormat(&c_dfDIMouse);
	if(result!=DI_OK)
		return false;

	//set cooperative
	result=_dxMouse->SetCooperativeLevel(hwnd,DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
	if(result!=DI_OK)
		return false;

	//acquire the mouse
	result=_dxMouse->Acquire();
	if(result!=DI_OK)
	{
		MessageBox(NULL, "Make sure the file arrows.bmp is in the current directory", "ERROR", MB_OK);
		return false;
	}
	_mouse_X=100;
	_mouse_Y=100;

	return true;
}
int CDxInput::OnKeyDown(int key)
{
	return ((!_oldKeys[key])&&(_keys[key] & 0x80));
}
int CDxInput::OnKeyPress(int key)
{
	return	(_keys[key] & 0x80);
}
int CDxInput::OnKeyUp(int key)
{
	return !(_keys[key] & 0x80);
}

long CDxInput::Mouse_X()
{
	return _mouseState.lX;
}

long CDxInput::Mouse_Y()
{
	return _mouseState.lY;
}

int CDxInput::Mouse_Button(int button)
{
	return BUTTON_DOWN(_mouseState,button);
}

void CDxInput::Pool_Mouse()
{
	_dxMouse->GetDeviceState(sizeof(_mouseState),(LPVOID)&_mouseState);
}
